package cn.tedu.pdfamily.product.controller;

import cn.tedu.pdfamily.product.pojo.dto.ReplyAddNewDTO;
import cn.tedu.pdfamily.product.pojo.dto.ReplyUpdateDTO;
import cn.tedu.pdfamily.product.pojo.vo.ReplyListItemVO;
import cn.tedu.pdfamily.product.pojo.vo.ReplyStandardVO;
import cn.tedu.pdfamily.product.service.IReplyService;
import cn.tedu.pdfamily.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 处理评论相关请求的控制器
 *
 * @author Half_Summer
 * @version 0.0.1a
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/replys")
@Api(tags = "07. 回复评论管理模块")
public class ReplyController {

    //注入replyService接口
    @Autowired
    private IReplyService replyService;
    private ReplyStandardVO reply;

    //输出日志: 对象已创建
    public ReplyController() {
        log.debug("业务对象创建: IReplyService.");
    }

    @ApiOperation("添加评论")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    //[方法] 添加评论
    public JsonResult<Void> addNew(@Valid ReplyAddNewDTO replyAddNewDTO) {
        log.debug("准备添加评论, 参数{}", replyAddNewDTO);
        replyService.addNewReply(replyAddNewDTO);
        log.debug("添加评论成功.");
        return JsonResult.ok();
    }

    @ApiOperation("根据id删除评论")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "评论id", required = true, dataType = "int")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> deleteById(@Range(min = 1, message = "删除评论失败, id不合法.") @PathVariable Long id) {
        log.debug("准备删除评论, id={}", id);
        replyService.delete(id);
        return JsonResult.ok();
    }

    @ApiOperation("修改评论详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "评论id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/update")
    public JsonResult<Void> updateById(@PathVariable Long id, ReplyUpdateDTO replyUpdateDTO) {
        log.debug("开始处理【修改评论详情】的请求，参数ID：{}, 新数据：{}", id, replyUpdateDTO);
        replyService.updateInfoById(id, replyUpdateDTO);
        return JsonResult.ok();
    }

    // http://localhost:9080/albums/9527
    @ApiOperation("根据id查询评论详情")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParam(name = "id", value = "评论id", required = true, dataType = "long")
    @GetMapping("/{id:[0-9]+}")
    public JsonResult<ReplyStandardVO> getStandardById(@PathVariable Long id) {
        log.debug("开始处理【根据id查询评论详情】的请求，参数：{}", id);
        ReplyStandardVO album = replyService.getStandardById(id);
        return JsonResult.ok(reply);
    }

    @ApiOperation("查询评论列表")
    @ApiOperationSupport(order = 420)
    @GetMapping("")
    public JsonResult<List<ReplyListItemVO>> list() {
        log.debug("开始处理【查询评论列表】的请求，无参数");
        List<ReplyListItemVO> list = replyService.list();
        return JsonResult.ok(list);
    }

}
